package com.shanling.shanlingcontroller.utils

import android.animation.Animator
import android.animation.ValueAnimator
import android.widget.ImageView
import com.shanling.shanlingcontroller.CustomApplication
import com.shanling.shanlingcontroller.R
import com.shanling.shanlingcontroller.bean.Constants
import com.shanling.shanlingcontroller.bean.EventCenter
import com.shanling.shanlingcontroller.bean.Music
import com.shanling.shanlingcontroller.music.event.PlaylistEvent
import com.shanling.shanlingcontroller.music.player.SongLoader
import com.shanling.shanlingcontroller.music.player.playqueue.PlayQueueManager
import org.greenrobot.eventbus.EventBus


object UIUtils {
    /**
     * 防止快速点击却换歌曲
     */
    private var lastClickTime: Long = 0

    @Synchronized
    fun isFastClick(): Boolean {
        val time = System.currentTimeMillis()
        if (time - lastClickTime < 500) {
            return true
        }
        lastClickTime = time
        return false
    }

    /**
     * 改变播放模式
     */
    fun updatePlayMode(imageView: ImageView, isChange: Boolean = false) {
        try {
            var playMode = PlayQueueManager.getPlayModeId()
            if (isChange) playMode = PlayQueueManager.updatePlayMode()
            when (playMode) {
                PlayQueueManager.PLAY_MODE_OREDE -> {
                    imageView.setImageResource(R.drawable.player_icon_order)
                    if (isChange) ToastUtils.showToast(CustomApplication.getInstance(), R.string.play_mode_in_order)
                }
                PlayQueueManager.PLAY_MODE_REPEAT -> {
                    imageView.setImageResource(R.drawable.player_icon_single)
                    if (isChange) ToastUtils.showToast(CustomApplication.getInstance(),R.string.play_mode_repeat)
                }
                PlayQueueManager.PLAY_MODE_RANDOM -> {
                    imageView.setImageResource(R.drawable.player_icon_shuffle)
                    if (isChange) ToastUtils.showToast(CustomApplication.getInstance(),R.string.play_mode_random)
                }
                PlayQueueManager.PLAY_MODE_LOOP -> {
                    imageView.setImageResource(R.drawable.player_icon_list_circulation)
                    if (isChange) ToastUtils.showToast(CustomApplication.getInstance(),R.string.play_mode_loop)
                }
            }
        } catch (e: Throwable) {

        }
    }

    /**
     * 收藏歌曲
     */
    fun collectMusic(imageView: ImageView, music: Music?) {
        music?.let {
            imageView.setImageResource(if (!it.isLove) R.drawable.player_icon_collect_selected else R.drawable.player_icon_collect_default)
        }
        ValueAnimator.ofFloat(1f, 1.3f, 0.8f, 1f).apply {
            duration = 600
            addUpdateListener {
                imageView.scaleX = it.animatedValue as Float
                imageView.scaleY = it.animatedValue as Float
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationRepeat(animation: Animator?) {
                }

                override fun onAnimationEnd(animation: Animator?) {
                    music?.let {
                        it.isLove = SongLoader.updateFavoriteSong(it)
                        EventBus.getDefault().post(EventCenter(Constants.PLAYLISTEVENT, PlaylistEvent(Constants.PLAYLIST_LOVE_ID, null)))
                    }
                }

                override fun onAnimationCancel(animation: Animator?) {
                }

                override fun onAnimationStart(animation: Animator?) {
                }

            })
        }.start()

    }

}










